## Viaplay have an API but it isn't for public use
module Shin
  module Play
    class Viaplay

      def new
        self
      end

      # Fix these before running
      def before(params={})
        raise MissingArgument, "You are missing the argument 'viaplay_country' which is required to use this source." unless Shin.get[:viaplay_country] != nil

        "https://content.viaplay."  + Shin.get[:viaplay_country] + "/pcdash-" + Shin.get[:viaplay_country] + "/"
      end

      # Series
      def series(params={})
        # domain
        domain = before()
        country = Shin.get[:viaplay_country]

        # Translated shit
        type = "kaikki"    if country == "fi"
        type = "alle"      if country == "dk" or country == "no"
        type = "samtliga"  if country == "se"

        # URLs
        if params[:page] != nil
          id = type + "?pageNumber=" + params[:page].to_s + "&sort=recently_added"
        elsif params[:season] != nil
          id = params[:id] + "?seasonNumber=" + params[:season].to_s + "&partial=1&block=2"
        elsif params[:id] != nil
          id = params[:id].to_s
        else
          id = type + "?sort=recently_added"
        end

        # Response
        if country == "fi"
          response = Base.get(domain + 'sarjat/' + id)
        else
          response = Base.get(domain + 'serier/' + id)
        end


        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          # Episodes for a seaosn is in products, not blocks.
          if params[:season] != nil
            data['_embedded']['viaplay:products'].to_hashugar
          else
            data['_embedded']['viaplay:blocks'].to_hashugar
          end
        else
          raise NotValid, "Couldn't parse the JSON"
        end

      end

      # Movies
      def movies(params={})
        # domain
        domain = before()
        country = Shin.get[:viaplay_country]

        # Translated shit
        type = "kaikki"    if country == "fi"
        type = "alle"      if country == "dk" or country == "no"
        type = "samtliga"  if country == "se"

        if params[:page] != nil
          id = type + "?pageNumber=" + params[:page].to_s + "&sort=recently_added"
        elsif params[:id] != nil
          id = params[:id].to_s
        else
          id = type + "?sort=recently_added"
        end



        # Response
        if country == "fi"
          response = Base.get(domain + 'leffat/' + id)
        elsif country == "no"
          response = Base.get(domain + 'filmer/' + id)
        else
          response = Base.get(domain + 'film/' + id)
        end


        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data['_embedded']['viaplay:blocks'].to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # Rental movies
      def rental_movies(params={})
        # domain
        domain = before()
        country = Shin.get[:viaplay_country]

        # Translated shit
        type = "kaikki"    if country == "fi"
        type = "alle"      if country == "dk" or country == "no"
        type = "samtliga"  if country == "se"

        if params[:page] != nil
          id = type + "?pageNumber=" + params[:page].to_s + "&sort=recently_added"
        elsif params[:id] != nil
          id = params[:id].to_s
        else
          id = type + "?sort=recently_added"
        end

        # Response
        if country == "fi"
          response = Base.get(domain + 'vuokraamo/' + id)
        elsif country == "dk"
          response = Base.get(domain + 'lejebutik/' + id)
        elsif country == "no"
          response = Base.get(domain + 'leiebutikk/' + id)
        else
          response = Base.get(domain + 'hyrbutik/' + id)
        end


        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data['_embedded']['viaplay:blocks'].to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # Errors
      class NotValid         < StandardError; end
      class MissingArgument  < StandardError; end
      class HTTPError        < StandardError; end
    end
  end
end
